package com.tracking.application.activities;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.os.Binder;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import com.followme.module.common.ReceiverManager;
import com.tracking.application.DataBase.DbAdapter;
import com.tracking.application.util.CommonModuleApp;

/**
 * Created with IntelliJ IDEA.
 * User: Kamil.O
 * Description: Service for main activity
 * Date: 26.11.13
 * Time: 00:29
 */
public class GpsTrackerService extends Service {
    private final IBinder mBinder = new MyBinder();
    private Context context;

    private double lastLattitude;
    private double lastLongtitude;

    protected boolean isInnerBroadcastReceiver = false;
    private long idRoad = -1;
    private DbAdapter db;

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d(CommonModuleApp.TAG_LOG_CAT, "onStartCommand GpsTrackerService");
        db = new DbAdapter(this);
        return Service.START_NOT_STICKY;
    }

    @Override
    public IBinder onBind(Intent arg0) {
        return mBinder;
    }

    public class MyBinder extends Binder {
        GpsTrackerService getService() {
            return GpsTrackerService.this;
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (isInnerBroadcastReceiver) unregisterReceiver(broadcastReceiver);
    }

    protected BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(CommonModuleApp.KEY_INTENT_ACTION_GPS)){

                Bundle bundle = intent.getExtras();
                double latitude = (Double) bundle.get(CommonModuleApp.KEY_GPS_LAT);
                double longitude = (Double) bundle.get(CommonModuleApp.KEY_GPS_LONG);

                float[] distance = new float[1];
                if (lastLattitude != 0 && lastLongtitude != 0) {
                    Location.distanceBetween(lastLattitude, lastLongtitude, latitude, longitude, distance);
                }

                float speed = (Float) bundle.get(CommonModuleApp.KEY_GPS_SPEED);
                double altitude = (Double) bundle.get(CommonModuleApp.KEY_GPS_ALTITUDE);

                if (speed != 0){
                    sendCoordinatesToMap(latitude, longitude);
                    saveCoordinatesToDb(latitude, longitude, speed, distance[0], altitude);
                }else{
                    saveCoordinatesToDb(latitude, longitude, 0, 0, altitude);
                }

                lastLattitude = latitude;
                lastLongtitude = longitude;
            }
        }
    };

    protected void saveCoordinatesToDb(double latitude, double longitude, float speed, float distance, double altitude){
        Log.d(CommonModuleApp.TAG_LOG_CAT, "Save coordinates to database");
        Log.d(CommonModuleApp.TAG_LOG_CAT, "latitude: "+latitude);
        Log.d(CommonModuleApp.TAG_LOG_CAT, "longitude: "+longitude);
        Log.d(CommonModuleApp.TAG_LOG_CAT, "speed: "+speed);
        Log.d(CommonModuleApp.TAG_LOG_CAT, "distance: "+distance);
        Log.d(CommonModuleApp.TAG_LOG_CAT, "altitude: "+altitude);

        db.open();
        db.insertLocation(latitude, longitude, this.idRoad, speed, distance, altitude);
        db.close();
    }

    protected void sendCoordinatesToMap(double latitude, double longitude){
        Log.d(CommonModuleApp.TAG_LOG_CAT, "Send coordinates to map");

        Intent intentTemp = new Intent();
        intentTemp.setAction(CommonModuleApp.KEY_COORDINATES_TO_MAP);
        intentTemp.putExtra(CommonModuleApp.KEY_GPS_LAT, latitude);
        intentTemp.putExtra(CommonModuleApp.KEY_GPS_LONG, longitude);

        context.sendBroadcast(intentTemp);
    }

    protected void updateEndDateRecording(){
        db.open();
        this.idRoad = db.updateEndDateRecording(String.valueOf(this.idRoad));
        db.close();
    }

    protected void registerReceiverFromGpsToService(String nameRoad, Context context){
        this.context = context;

        db.open();
        this.idRoad = db.insertRoad(nameRoad);
        db.close();

        ReceiverManager.registerBroadcatReceiver(this, CommonModuleApp.KEY_INTENT_ACTION_GPS, broadcastReceiver);
        isInnerBroadcastReceiver = true;
    }

    protected void unregisteredReceiverFromGpsToService(){
        ReceiverManager.unregisterReceiver(this, broadcastReceiver);
        isInnerBroadcastReceiver = false;
    }

    public long getIdRoad() {
        return idRoad;
    }
}
